var MusicPlayer = {
	Obj : null,
	List : null,
	View : [
			'<section id="music_player_ctrl" class="music_player_ctrl">',
			'	<p id="music_player_desc" class="music_player_desc"></p>',
			'	<p id="music_player_prog" class="music_player_prog"><em style="width:1px"></em></p>',
			'	<p id="music_player_prog_data" class="music_player_prog_data">00:00/00:00</p>',
			'	<p>',
			'		<span id="music_player_volume" class="music_player_volume">',
			'			<strong>音量控制:</strong><span id="music_player_volume_show"><em style="width:100%"></em></span>',
			'		</span>',
			'		<span id="music_player_muted" class="music_player_muted">静音</span>',
			'		<br style="clear:both" />',
			'	</p>',
			'	<section class="music_player_btns">',
			'		<span id="MusicPrew" class="prew">上首</span>',
			'		<span id="MusicPlay" class="play">暂停</span>',
			'		<span id="MusicNext" class="next">下首</span>	',
			'		<span id="MusicCircle" class="circle now">循环播放</span>	',
			'		<span id="MusicSingle" class="single">单曲循环</span>',
			'	</section>',
			'</section>'].join(""),		
	Para : {
		Selected : null,
		Index : 0,
		Vloum : 0.5,
		Mode : 'circle'
		},	
	Btns : {},
	Funs : {
		playThis : function(target,src,title){
			document.title = title; 
			MusicPlayer.Btns._desc.innerHTML = title;
			
			if(MusicPlayer.Para.Selected !== null) {
				MusicPlayer.Para.Selected.parentNode.className = MusicPlayer.Para.Selected.parentNode.className.replace("now","");
			}
			target.parentNode.className += " now";
			MusicPlayer.Para.Selected = target;
			var len = MusicPlayer.Para.Len;
			var as = MusicPlayer.Para.As;
			for(var n=0;n<len;n++){
				if (as[n] == target){
					MusicPlayer.Para.Index = n;	
				}	
			}
			
			MusicPlayer.Obj.src = src;
			MusicPlayer.Obj.title = title;
			MusicPlayer.Obj.load();
			MusicPlayer.Obj.autoplay = 'true';
			},
		showProg : function(){
			var audio = MusicPlayer.Obj;
			if(timeFormat(audio.duration)<=0){return false;}
			var objD = MusicPlayer.Btns._time;
			var objP = MusicPlayer.Btns._prog;
			var w = parseInt(objP.clientWidth);
				objD.innerHTML = ['<em>',timeFormat(audio.currentTime),'</em>/',timeFormat(audio.duration)].join("");
				objP.getElementsByTagName("em")[0].style.width = w*audio.currentTime/audio.duration + "px";
			},	
		ctroProg : function(event){
			var audio = MusicPlayer.Obj;
			var v1 =  getOffsetX(MusicPlayer.Btns._prog,event);
			audio.currentTime = audio.duration*v1;	
			},
		ctroVolum : function(event){
			var v1 =  getOffsetX(MusicPlayer.Btns._soud,event);
			MusicPlayer.Funs.setVolum(v1);	
			},	
		setVolum : function(v){
			MusicPlayer.Obj.volume = v;
			var w = parseInt(MusicPlayer.Btns._soud.clientWidth);
			MusicPlayer.Btns._soud.getElementsByTagName("em")[0].style.width = w*v + "px";
			},		
		playPrew : function (){
			if (MusicPlayer.Para.Index == 0 ){MusicPlayer.Para.Index = MusicPlayer.Para.Len}
			MusicPlayer.Para.Index = MusicPlayer.Para.Index - 1;
			var n =  MusicPlayer.Para.Index;
			var obj = MusicPlayer.Para.As[n];
			var src = obj.href;
			var title = obj.innerHTML;
			MusicPlayer.Funs.playThis(obj,src,title);
			},
		playNext : function(){
			MusicPlayer.Para.Index = (MusicPlayer.Para.Index+1)%MusicPlayer.Para.Len;
			var n =  MusicPlayer.Para.Index;
			var obj = MusicPlayer.Para.As[n];
			var src = obj.href;
			var title = obj.innerHTML;
			MusicPlayer.Funs.playThis(obj,src,title);
			},
		playStop : function(){
			var audio = MusicPlayer.Obj;
			if (audio.autoplay) {
				audio.pause();
				audio.autoplay = false;
				MusicPlayer.Btns._play.innerHTML = '播放';
			}else{
				audio.play();
				audio.autoplay = true;
				MusicPlayer.Btns._play.innerHTML = '暂停';
			}	
			},
		playEnd : function(){
			if (MusicPlayer.Para.Mode == "single"){
				var n =  MusicPlayer.Para.Index;
				var obj = MusicPlayer.Para.As[n];
				var src = obj.href;
				var title = obj.innerHTML;
				MusicPlayer.Funs.playThis(obj,src,title);
			}
			else{MusicPlayer.Funs.playNext()}
			},
		setMode : function(obj,mode){
			MusicPlayer.Para.Mode = mode;
			MusicPlayer.Btns._sgle.className = MusicPlayer.Btns._sgle.className.replace("now","");
			MusicPlayer.Btns._cile.className = MusicPlayer.Btns._cile.className.replace("now","");
			obj.className += " now";
			},
		playMuted : function(){
			var audio = MusicPlayer.Obj;
			var muted = MusicPlayer.Btns._mute;
			if (muted.innerHTML.indexOf("span")<0){
				audio.muted = true;
				muted.innerHTML = '<span>静音</span>';
			}else{
				audio.muted = false;
				muted.innerHTML = '静音';
			}
			},
		Error : function(){
			return false;
			}	
	},
	Build : function(){
		var ObjUI = document.createElement("div");
		ObjUI.innerHTML = this.View;
		insertAfter(ObjUI,this.Obj);
		this.Btns._play = document.getElementById("MusicPlay");
		this.Btns._prew = document.getElementById("MusicPrew");
		this.Btns._next = document.getElementById("MusicNext");
		this.Btns._sgle = document.getElementById("MusicSingle");
		this.Btns._cile = document.getElementById("MusicCircle");
		
		this.Btns._desc = document.getElementById("music_player_desc");
		this.Btns._prog = document.getElementById("music_player_prog");
		this.Btns._time = document.getElementById("music_player_prog_data");
		this.Btns._soud = document.getElementById("music_player_volume_show");
		this.Btns._mute = document.getElementById("music_player_muted");
		this.Para.As = this.List.getElementsByTagName("a");
		this.Para.Len = this.List.getElementsByTagName("a").length;
	},
	Attach : function(){	
		var _this = this;
		var CtrlObj = document.getElementById("music_player_ctrl");
		_this.List.onclick = function(event){
			event = event || window.event;
			var target = event.target || event.srcElement;
			var tags = target.tagName.toLowerCase();
			if(tags == 'a'){
				_this.Funs.playThis(target,target.href,target.innerHTML);
				return false; 
			}
		};
		CtrlObj.onclick = function(event){
			event = event || window.event;
			var target = event.target || event.srcElement;
			if(target == _this.Btns._play){
				_this.Funs.playStop();
			}
			if(target == _this.Btns._prew){
				_this.Funs.playPrew();
			}
			if(target == _this.Btns._next){
				_this.Funs.playNext();
			}
			if(target == _this.Btns._sgle){
				_this.Funs.setMode(target,'single');
			}
			if(target == _this.Btns._cile){
				_this.Funs.setMode(target,'circle');
			}
			if(target == _this.Btns._prog || target.parentNode == _this.Btns._prog){
				_this.Funs.ctroProg(event);
			}
			if(target == _this.Btns._soud || target.parentNode == _this.Btns._soud){
				_this.Funs.ctroVolum(event);
			}
			if(target == _this.Btns._mute || target.parentNode == _this.Btns._mute){
				_this.Funs.playMuted();
			}
		}	
		var target = _this.Para.As[_this.Para.Index];
		_this.Funs.playThis(target,target.href,target.innerHTML);
		_this.Funs.setVolum(_this.Para.Vloum);
	},
	Init : function(){
		this.Build();
		this.Attach();
	}			
};